package com.studorm.xml.action;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import com.opensymphony.xwork2.ActionSupport;
import com.studorm.XMLOption;


/**
 * @author Waiting_hao
 *
 */
public class DormAction extends ActionSupport {
	private List<String> options = new ArrayList<String>();
	public List<String> getOptions() {
		return options;
	}

	public void setOptions(List<String> options) {
		this.options = options;
	}

	private static final long serialVersionUID = 2811028652992276554L;
	//http://www.cnblogs.com/codeplus/archive/2011/07/18/2109316.html
	public String loadDormNuByName() throws Exception{// 通过宿舍楼名获得宿舍号
		//读取请求的参数 
		HttpServletRequest request = ServletActionContext.getRequest(); 
		String dormBdName = request.getParameter("dornBdName"); // 得到宿舍楼名

		String path=request.getSession().getServletContext().getRealPath("\\db\\xml\\dorminfs.xml");
		Document doc = XMLOption.loadXML(path);
		List<? extends Node> list = doc.selectNodes("/dorminfs/dorminf[dormBdName='"+dormBdName+"']/dormNu");

		for(Node s:list){
			String d = s.getText();
			options.add(d);
		}

		return SUCCESS;
	}

	public String loadDormBdName() throws DocumentException{// 获得宿舍楼名

		HttpServletRequest request = ServletActionContext.getRequest(); 
		String path=request.getSession().getServletContext().getRealPath("\\db\\xml\\dorminfs.xml");
		Document doc = XMLOption.loadXML(path);
		List<? extends Node> list = doc.selectNodes("/dorminfs/dorminf/dormBdName");
		
		/* --------------------------------------
		 * 	从XML获取宿舍楼名并添加到options ver.12.18
		 * --------------------------------------
		 *  若options中有相同数据，则不添加到
		 *  options
		 *  				by Huang
		 * 
		 */
		for(Node s:list){
			String d = s.getText();
			if(!options.contains(d)){
				options.add(d);
			}
		}
		

		return SUCCESS;
	}

	public String loadUtilYear() throws Exception{// 获得水电费年份
		HttpServletRequest request = ServletActionContext.getRequest(); 
		String path=request.getSession().getServletContext().getRealPath("\\db\\xml\\utilities.xml");
		Document doc = XMLOption.loadXML(path);
		List<? extends Node> list = doc.selectNodes("/utilities/utility/date");
		
		/* --------------------------------------
		 * 	从XML获取水电费年份并添加到options ver.12.18
		 * --------------------------------------
		 *  若options中有相同数据，则不添加到
		 *  options
		 *  				by Huang
		 * 
		 */
		for(Node s:list){
			String d = s.getText().substring(0, 4);
			if(!options.contains(d)){
				options.add(d);
			}
		}

		return SUCCESS;
	}
}
